package com.example.user3.demo.wiget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import com.example.user3.demo.R;

public class CountDownImage extends View {

    public int color= Color.parseColor("#000000") ;
    private float mProgress;      // 圆环进度(0-100)
    Paint mProgPaint;
    Paint paint;
    private void initView() {
        mProgPaint= new Paint();
        mProgPaint.setColor(color);
        mProgPaint.setStyle(Paint.Style.FILL );    // 只描边，不填充
        mProgPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mProgPaint.setAntiAlias(true);              // 设置抗锯齿
        mProgPaint.setDither(true);

        paint =new Paint();
//        paint.setStyle(Paint.Style.FILL );    // 只描边，不填充
//        paint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        paint.setAntiAlias(true);              // 设置抗锯齿
        paint.setDither(true);
//        setBackgroundColor(color);

        Canvas canvas=new Canvas();
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hb);
        Log.i("zzy",bitmap.toString()+"--"+bitmap.getWidth());
        mOut = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Log.i("zzy",mOut.toString()+"--"+mOut.getWidth());


        girlBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hb);
        girlBitWidth = girlBitmap.getWidth();
        girlBitHeight = girlBitmap.getHeight();
    }

    public CountDownImage(Context context) {
        super(context);
        initView();
    }

    public CountDownImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CountDownImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    Bitmap bitmap,mOut;
    @Override
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);



    }
    private Bitmap girlBitmap;
    private Rect girlSrcRect , girlDesRect;
    private int mTotalWidth, mTotalHeight;
    private int girlBitWidth , girlBitHeight;
    TextView s;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.d("xxxxxx", "onSizeChanged , w = "+w+" , h = "+h+" , mBitWidth = "+girlBitWidth+" , mBitHeight = "+girlBitHeight);
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;

        girlSrcRect = new Rect(0, 0, girlBitWidth, girlBitHeight);
        girlDesRect = new Rect(0, 0, girlBitWidth, girlBitHeight);

    }
    /**
     * 设置当前进度，并展示进度动画。如果动画时间小于等于0，则不展示动画
     *
     * @param progress 当前进度（0-100）
     * @param animTime 动画时间（毫秒）
     */
    @androidx.annotation.RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public void setProgress(float progress, long animTime) {
        if (animTime <= 0) setProgress(progress);
        else {
            ValueAnimator animator = ValueAnimator.ofFloat(mProgress, progress);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mProgress = (float) animation.getAnimatedValue();
                    invalidate();
                }

            });
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(animTime);
            animator.start();
        }
    }
    /**
     * 设置当前进度
     *
     * @param progress 当前进度（0-360）
     */
    public void setProgress(float progress) {
        this.mProgress = progress;
        invalidate();
    }
}
